package com.centon.auto_code.project.service.impl;

import com.centon.auto_code.comm.ServiceCode;
import com.centon.auto_code.domain.*;
import com.centon.auto_code.domain.gen.*;
import com.centon.auto_code.mapper.*;
import com.centon.auto_code.project.service.ProjectService;
import com.centon.auto_code.project.vo.ProFileVo;
import com.centon.auto_code.project.vo.ProProjectVo;
import com.centon.auto_code.project.vo.ProTableVo;
import com.centon.auto_code.util.BeanUtil;
import com.centon.auto_code.util.CommUtils;
import com.centon.auto_code.util.Constants;
import com.centon.auto_code.util.TypeEnum;
import com.centon.auto_code.util.TypeEnum.PackageTypeEnum;
import com.centon.auto_code.vo.BaseRes;
import com.centon.auto_code.vo.PageData;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProjectServiceImpl implements ProjectService {


    private static final Logger LOG = LoggerFactory.getLogger(ProjectServiceImpl.class);

    @Resource
    private ProProjectMapper proProjectMapper;

    @Resource
    private ProProjectDatabaseRelMapper proProjectDatabaseRelMapper;

    @Autowired
    ProFileMapper proFileMapper;

    @Autowired
    ProFieldMapper proFieldMapper;

    @Autowired
    ProTableColumnMapper proTableColumnMapper;

    @Autowired
    ProTableMapper proTableMapper;

    @Autowired
    ProModuleMapper proModuleMapper;

    @Resource
    ProMicroServiceMapper microServiceMapper;

    @Override
    public BaseRes<PageData<ProProjectVo>> entityPage(ProProjectVo proProjectVo) {

        BaseRes<PageData<ProProjectVo>> res = new BaseRes<>();
        res.setData(new PageData<>());
        res.getData().setCurrent(proProjectVo.getCurrent());
        res.getData().setPageSize(proProjectVo.getPageSize());

        ProProjectCriteria example = new ProProjectCriteria();
        int mysqlLength = res.getData().getPageSize();
        int mysqlOffset = (res.getData().getCurrent() - 1) * mysqlLength;

        example.setMysqlLength(mysqlLength);
        example.setMysqlOffset(mysqlOffset);

        ProProjectCriteria.Criteria mCriteria = example.createCriteria();
        mCriteria.andDelFlagEqualTo(0);
        if (StringUtils.isNotEmpty(proProjectVo.getName())) {
            mCriteria.andNameLike(proProjectVo.getName() + "%");
        }

        long totalCount = proProjectMapper.countByExample(example);
        res.getData().setTotal(totalCount);
        if (totalCount > 0) {
            List<ProProject> proProjectList = proProjectMapper.selectByExample(example);
            List<ProProjectVo> rows = BeanUtil.voConvertList(proProjectList, ProProjectVo.class);
            res.getData().setRows(rows);
        }
        return res;
    }

    @Override
    public BaseRes<ProProjectVo> getEntityInfo(String id) {
        ProProject mProProject = proProjectMapper.selectByPrimaryKey(id);
        if (mProProject != null) {
            ProProjectVo mProProjectVo = BeanUtil.voConvert(mProProject, ProProjectVo.class);

            List<String> dbIds = new ArrayList<>();

            // query project ref db list
            ProProjectDatabaseRelCriteria mProProjectDatabaseRelCriteria = new ProProjectDatabaseRelCriteria();
            mProProjectDatabaseRelCriteria.createCriteria().andProjectIdEqualTo(mProProjectVo.getId());
            List<ProProjectDatabaseRel> list = proProjectDatabaseRelMapper.selectByExample(mProProjectDatabaseRelCriteria);
            for (int i = 0; i < list.size(); i++) {
                dbIds.add(list.get(i).getDatabaseId());
            }

            mProProjectVo.setDbIds(dbIds);
            return new BaseRes<>(mProProjectVo, ServiceCode.SUCCESS);
        }
        return new BaseRes<>(ServiceCode.NOT_FIND_ENTITY);
    }

    @Override
    public BaseRes<String> addEntityInfo(ProProjectVo proProjectVo) {
        proProjectVo.setId(CommUtils.randomUUID());
        ProProject proProject = BeanUtil.voConvert(proProjectVo, ProProject.class);
        proProject.setCreateTime(new Date());
        proProject.setUpdateTime(new Date());
        proProject.setIgnoreTables("");

        proProjectMapper.insertSelective(proProject);

        // insert project and database
        if (null != proProjectVo.getDbIds()) {
            updateProjectAndDatabaseRel(proProject.getId(), proProjectVo.getDbIds());
        }

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> modifyEntityInfo(ProProjectVo proProjectVo) {
        ProProject proProject = BeanUtil.voConvert(proProjectVo, ProProject.class);
        proProject.setUpdateTime(new Date());
        proProjectMapper.updateByPrimaryKeySelective(proProject);

        if (null != proProjectVo.getDbIds()) {
            updateProjectAndDatabaseRel(proProject.getId(), proProjectVo.getDbIds());
        }

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> delEntityInfo(String id) {

        ProProject record = new ProProject();
        record.setId(id);
        record.setDelFlag(Constants.DelFlag.del.code);
        // delete module
        proProjectMapper.updateByPrimaryKeySelective(record);

        return new BaseRes<>(ServiceCode.SUCCESS);
    }

    @Override
    public BaseRes<String> genProject(ProProjectVo proProjectVo) {
        return null;
    }

    private class TreeId {
        public Integer index = 1;
    }

    @Override
    public BaseRes<String> fetchProjectFileTree(String projectId, String packageType, String moduleId) {
        ProProject mProProject = proProjectMapper.selectByPrimaryKey(projectId);

        if (mProProject == null) {
            return new BaseRes<>(ServiceCode.NOT_EXISTS_PROJECT);
        }

        TreeId treeId = new TreeId();
        JSONArray rootArray = new JSONArray();

        /** 微服务项目 */
        if("micro".equalsIgnoreCase(mProProject.getProType())) {

            ProMicroServiceCriteria example = new ProMicroServiceCriteria();
            ProMicroServiceCriteria.Criteria criteria = example.createCriteria();
            criteria.andDelFlagEqualTo(0).andProjectIdEqualTo(projectId);
            example.setOrderByClause(" name asc ");

            if (!"0".equals(moduleId) && !"-1".equals(moduleId)){
                ProModule proModule = proModuleMapper.selectByPrimaryKey(moduleId);
                String microId = proModule.getMicroId();
                criteria.andIdEqualTo(microId);
            }

            List<ProMicroService> micros = microServiceMapper.selectByExample(example);

            for(ProMicroService ms: micros) {
                JSONObject project = getProjectTreeBy(treeId, mProProject, packageType, moduleId, rootArray, ms);
                rootArray.put(project);
            }

        } else {
            JSONObject project = getProjectTreeBy(treeId, mProProject, packageType, moduleId, rootArray, null);
            rootArray.put(project);
        }

        return new BaseRes<>(rootArray.toString(), ServiceCode.SUCCESS);
    }


    private JSONObject getProjectTreeBy(TreeId treeId, ProProject mProProject, String packageType, String moduleId, JSONArray rootArray, ProMicroService microService) {

        String projectId = mProProject.getId();

        ProModule commProModule = new ProModule();
        /**
         * moduleId: -1:为公共模块, 0:全部 example: com.test.controller
         */
        String microId = "0";
        if(null != microService) {
            microId = microService.getId();
            commProModule.setMicroId(microId);
        }

        List<ProModule> modules = new ArrayList<>();
        if (moduleId == null || "0".equals(moduleId)) {
            // fetch All module
            ProModuleCriteria mProModuleCriteria = new ProModuleCriteria();
            ProModuleCriteria.Criteria mCriteria = mProModuleCriteria.createCriteria();
            mCriteria.andProjectIdEqualTo(projectId).andDelFlagEqualTo(0);

            if(null != microService) {
                mCriteria.andMicroIdEqualTo(microService.getId());
                microId = microService.getId();
            }

            modules = proModuleMapper.selectByExample(mProModuleCriteria);
            modules.add(commProModule);

        } else if ( "-1".equals(moduleId)) {

            modules.add(commProModule);

        } else {
            // fetch one module
            modules = new ArrayList<ProModule>();
            ProModule mProModule = proModuleMapper.selectByPrimaryKey(moduleId);
            if (mProModule !=null){
                modules.add(mProModule);
            }
        }

        try {

            String srcName = "src";
            if(null != microService) {
                srcName = microService.getArtifactId()+"("+microService.getName()+")";
            }

            JSONObject src = newJson(treeId, null, srcName, "folder", microId);
            src.put("open", true);

            if (PackageTypeEnum.Flat.name().equals(packageType)) {
                processProjectByFlat(rootArray, treeId, src, mProProject, modules, microService);
            } else {
                processProjectByHierarchical(rootArray, treeId, src, mProProject, modules, microService);
            }

            return src;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    /** Package Type Hierarchical */
    private void processProjectByHierarchical(JSONArray rootArray, TreeId treeId, JSONObject src, ProProject proProject, List<ProModule> modules, ProMicroService microService) throws JSONException {

        // root module-_package
        String projectId = proProject.getId();
        String groupId = proProject.getGroupId();
        String artifctId = proProject.getArtifactId();

        String microId = "0";
        String basePkgName = groupId + "." + artifctId;
        if(microService != null) {
            microId = microService.getId();
            basePkgName = groupId + "." + microService.getArtifactId().replaceAll("-", "_");
        }
        int pId = src.getInt("id");

        JSONObject artifctIdNode = newJson(treeId, pId, basePkgName, "package", microId);
        artifctIdNode.put("open", true);
        pId = artifctIdNode.getInt("id");
        rootArray.put(artifctIdNode);

        JSONObject controllerNode = null;
        JSONObject serviceNode = null;
        JSONObject mapperNode = null;
        JSONObject voNode = null;
        JSONObject domainNode = null;

        for (int i = 0; i < modules.size(); i++) {

            ProModule module = modules.get(i);

            // query file list, the same controller\service\vo\dmain\mapper\page
            ProFileCriteria mProFileCriteria = new ProFileCriteria();
            ProFileCriteria.Criteria mCriteria = mProFileCriteria.createCriteria();
            LOG.info("module={}",module);
            String moduleMicroId = module.getMicroId();

            // id > 0  ! "0".equals(module.getId());
            if (null != module.getId() && ! "0".equals(module.getId()) ) {
                mCriteria.andProjectIdEqualTo(projectId);
                mCriteria.andModuleIdEqualTo(module.getId());
                if(moduleMicroId != null) {
                    // 单项目为空
                    mCriteria.andMicroIdEqualTo(moduleMicroId);
                }
            } else {

                // 或者是当前项目的模块ID为空 - - - 多服务问题
                ProFileCriteria.Criteria orCriteria1 = mProFileCriteria.or();
                //ProFileCriteria.Criteria orCriteria2 = mProFileCriteria.or();

                // 所有项目公用 projectId为空
                mCriteria.andProjectIdIsNull();

                orCriteria1.andModuleIdEqualTo("0").andProjectIdEqualTo(projectId);

                if(moduleMicroId != null) {
                    // 单项目为空
                    orCriteria1.andMicroIdEqualTo(moduleMicroId);
//					orCriteria2.andMicroIdEqualTo(moduleMicroId);
                }
            }

            List<ProFile> proFiles = proFileMapper.selectByExample(mProFileCriteria);

            LOG.info("proFiles-size={}", proFiles.size());

            Integer modulePid = pId;

            // package -_ file
            if (Constants.ProjectModuleTypeEnum.standard.name().equals(proProject.getPackageType())) {
                if (controllerNode == null) {
                    controllerNode = newJson(treeId, modulePid, "controller", "package", microId);
                    serviceNode = newJson(treeId, modulePid, "service", "package", microId);
                    mapperNode = newJson(treeId, modulePid, "mapper", "package", microId);
                    voNode = newJson(treeId, modulePid, "vo", "package", microId);
                    domainNode = newJson(treeId, modulePid, "domain", "package", microId);

                    rootArray.put(controllerNode);
                    rootArray.put(serviceNode);
                    rootArray.put(mapperNode);
                    rootArray.put(voNode);
                    rootArray.put(domainNode);
                }
            } else {
                String _module = module.getName();
                if (StringUtils.isNotEmpty(_module)) {
                    JSONObject moduleNode = newJson(treeId, pId, _module, "package", microId);
                    modulePid = moduleNode.getInt("id");
                    rootArray.put(moduleNode);
                } else {
                    _module = "";
                }

                controllerNode = newJson(treeId, modulePid, _module, "controller", "package", microId);

                LOG.info("_module={}", _module);

                serviceNode = newJson(treeId, modulePid, _module, "service", "package", microId);
                mapperNode = newJson(treeId, modulePid, _module, "mapper", "package", microId);
                voNode = newJson(treeId, modulePid, _module, "vo", "package", microId);
                domainNode = newJson(treeId, modulePid, _module, "domain", "package", microId);

                rootArray.put(controllerNode);
                rootArray.put(serviceNode);
                rootArray.put(mapperNode);
                rootArray.put(voNode);
                rootArray.put(domainNode);
            }

            Integer controllerNodeId = controllerNode.getInt("id");
            Integer serviceNodeId = serviceNode.getInt("id");
            Integer mapperNodeId = mapperNode.getInt("id");
            Integer voNodeId = voNode.getInt("id");
            Integer domainNodeId = domainNode.getInt("id");

            for (ProFile file : proFiles) {

                // module name within in package name
                LOG.info("fileName={}", file.getName());

                String fileType = file.getFileType();
                String classType = file.getClassType();
                JSONObject temp = null;
                Integer _pId = null;

                if (TypeEnum.FileTypeEnum.controller.eq(fileType)) {
                    _pId = controllerNodeId;
                } else if (TypeEnum.FileTypeEnum.service.eq(fileType)) {
                    _pId = serviceNodeId;
                } else if (TypeEnum.FileTypeEnum.mapper.eq(fileType)) {
                    _pId = mapperNodeId;
                } else if (TypeEnum.FileTypeEnum.vo.eq(fileType)) {
                    _pId = voNodeId;
                } else if (TypeEnum.FileTypeEnum.domain.eq(fileType)) {
                    _pId = domainNodeId;
                }


                temp = newJson(_pId, file.getName() + ".java", fileType, file.getId(), microId,treeId);
                temp.put("classType", classType);
                rootArray.put(temp);
            }
        }

    }


    /** Package Type Flat */
    private void processProjectByFlat(JSONArray rootArray, TreeId treeId, JSONObject src, ProProject proProject, List<ProModule> modules, ProMicroService microService)
            throws JSONException {

        String microId = "0";
        String projectId = proProject.getId();
        String groupId = proProject.getGroupId();
        String artifctId = proProject.getArtifactId();

        String basePkgName = groupId + "." + artifctId;
        if(microService != null) {
            microId = microService.getId();
            basePkgName = groupId + "." + microService.getArtifactId().replaceAll("-", "_");
        }

        int srcId = src.getInt("id");

        JSONObject controllerNode = null;
        JSONObject serviceNode = null;
        JSONObject mapperNode = null;
        JSONObject voNode = null;
        JSONObject domainNode = null;

        for (ProModule module : modules) {

            String moduleId = module.getId();

            // query file list, the same controller\service\vo\dmain\mapper\page
            ProFileCriteria mProFileCriteria = new ProFileCriteria();
            ProFileCriteria.Criteria mCriteria = mProFileCriteria.createCriteria();

            String moduleMicroId = module.getMicroId();
            if(moduleMicroId != null) {
                // 单项目为空
                mCriteria.andMicroIdEqualTo(moduleMicroId);
            }

            if (moduleId != null && !"0".equals(moduleId)) {
                mCriteria.andProjectIdEqualTo(projectId);
                mCriteria.andModuleIdEqualTo(moduleId);
            } else {

                // 所有项目公用 projectId为空
                mCriteria.andProjectIdIsNull();

                // 或者是当前项目的模块ID为空
                mProFileCriteria.or().andModuleIdIsNull().andProjectIdEqualTo(projectId);
            }

            List<ProFile> proFiles = proFileMapper.selectByExample(mProFileCriteria);

            // package -_ file

            if (Constants.ProjectModuleTypeEnum.standard.name().equals(proProject.getPackageType())) {
                if (controllerNode == null) {
                    controllerNode = newJson(treeId, srcId, basePkgName + ".controller", "package", microId);
                    serviceNode = newJson(treeId, srcId, basePkgName + ".service", "package", microId);
                    mapperNode = newJson(treeId, srcId, basePkgName + ".mapper", "package", microId);
                    voNode = newJson(treeId, srcId, basePkgName + ".vo", "package", microId);
                    domainNode = newJson(treeId, srcId, basePkgName + ".domain", "package", microId);

                    rootArray.put(controllerNode);
                    rootArray.put(serviceNode);
                    rootArray.put(mapperNode);
                    rootArray.put(voNode);
                    rootArray.put(domainNode);
                }
            } else {
                String _basePkgName = basePkgName;
                if (StringUtils.isNotEmpty(module.getName())) {
                    _basePkgName = _basePkgName + "." + module.getName();
                }

                controllerNode = newJson(treeId, srcId, _basePkgName + ".controller", "package", microId);
                serviceNode = newJson(treeId, srcId, _basePkgName + ".service", "package", microId);
                mapperNode = newJson(treeId, srcId, _basePkgName + ".mapper", "package", microId);
                voNode = newJson(treeId, srcId, _basePkgName + ".vo", "package", microId);
                domainNode = newJson(treeId, srcId, _basePkgName + ".domain", "package", microId);

                rootArray.put(controllerNode);
                rootArray.put(serviceNode);
                rootArray.put(mapperNode);
                rootArray.put(voNode);
                rootArray.put(domainNode);
            }

            Integer controllerNodeId = controllerNode.getInt("id");
            Integer serviceNodeId = serviceNode.getInt("id");
            Integer mapperNodeId = mapperNode.getInt("id");
            Integer voNodeId = voNode.getInt("id");
            Integer domainNodeId = domainNode.getInt("id");

            for (ProFile file : proFiles) {

                System.out.println("fileName=" + file.getName());
                // module name within in package name

                String fileType = file.getFileType();
                String classType = file.getClassType();
                JSONObject temp = null;
                Integer pId = null;

                if (TypeEnum.FileTypeEnum.controller.eq(fileType)) {
                    pId = controllerNodeId;
                } else if (TypeEnum.FileTypeEnum.service.eq(fileType)) {
                    pId = serviceNodeId;
                } else if (TypeEnum.FileTypeEnum.mapper.eq(fileType)) {
                    pId = mapperNodeId;
                } else if (TypeEnum.FileTypeEnum.vo.eq(fileType)) {
                    pId = voNodeId;
                } else if (TypeEnum.FileTypeEnum.domain.eq(fileType)) {
                    pId = domainNodeId;
                }

                temp = newJson( pId, file.getName() + ".java", fileType, file.getId(), microId,treeId);
                temp.put("classType", classType);
                rootArray.put(temp);
            }
        }
    }

    private JSONObject newJson(Integer pId, String name, String fileType, String fileId, String microId ,TreeId treeId) throws JSONException {
        return this.newJson(treeId, pId, "", name, fileType, fileId, microId);
    }

    private JSONObject newJson(TreeId treeId, Integer pId, String name, String fileType, String microId) throws JSONException {
        return this.newJson(treeId, pId, "", name, fileType, "", microId);
    }

    private JSONObject newJson(TreeId treeId, Integer pId, String moduleName, String name, String fileType, String microId) throws JSONException {
        return this.newJson(treeId, pId, moduleName, name, fileType, "", microId);
    }

    private JSONObject newJson(TreeId treeId, Integer pId, String moduleName, String name, String fileType, String fileId, String microId)
            throws JSONException {
        treeId.index++;

        JSONObject packageNode = new JSONObject();
        packageNode.put("id", treeId.index);
        packageNode.put("indentify", moduleName + "_" + name);
        packageNode.put("pId", pId);
        packageNode.put("fileId", fileId);
        packageNode.put("name", name);
        packageNode.put("fileType", fileType);
        packageNode.put("microId", microId);
        return packageNode;

    }



    @Override
    public BaseRes<List<ProFileVo>> fetchFileByClassType(String projectId, String fileType) {
        return null;
    }

    @Override
    public BaseRes<String> addFile(ProFileVo proFileVo) {
        proFileVo.setCreateTime(new Date());
        proFileVo.setUpdateTime(new Date());
        proFileVo.setId(CommUtils.randomUUID());

        // 公共模块
        if (proFileVo.getModuleId() == null || "".equals(proFileVo.getModuleId())) {
            proFileVo.setModuleId(null);
        }

        BaseRes<String> res = new BaseRes<String>();

        int effect = proFileMapper.insertSelective(proFileVo);
        if (effect > 0) {
            res.setServiceCode(ServiceCode.SUCCESS);
        } else {
            res.setServiceCode(ServiceCode.FAILED);
        }
        return res;
    }

    @Override
    public BaseRes<ProFileVo> modifyFileInfo(ProFileVo proFileVo) {
        int effected = proFileMapper.updateByPrimaryKeySelective(proFileVo);
        LOG.info("effected={}", effected);

        BaseRes<ProFileVo> res = new BaseRes<ProFileVo>();
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }

    @Override
    public BaseRes<ProFileVo> fetchFileInfo(String fileId) {
        BaseRes<ProFileVo> res = new BaseRes<ProFileVo>();
        ProFile proFile = proFileMapper.selectByPrimaryKey(fileId);

        ProFileVo mProFileVo = BeanUtil.voConvert(proFile, ProFileVo.class);

        ProFieldCriteria mProFieldCriteria = new ProFieldCriteria();
        mProFieldCriteria.createCriteria().andFileIdEqualTo(fileId);
        List<ProField> proFields = proFieldMapper.selectByExample(mProFieldCriteria);

        mProFileVo.setFields(proFields);
        if (proFile.getSuperId() != null && !"".equals(proFile.getSuperId())) {

            if (com.centon.auto_code.util.mbg.Constants.FileType.VO.equals(proFile.getFileType())) {

                // fetch table
                ProTable proTable = proTableMapper.selectByPrimaryKey(proFile.getSuperId());

                ProTableColumnCriteria mProTableColumnCriteria = new ProTableColumnCriteria();
                mProTableColumnCriteria.createCriteria().andTableIdEqualTo(proTable.getId());
                List<ProTableColumn> columns = proTableColumnMapper.selectByExample(mProTableColumnCriteria);
                ProTableVo table = BeanUtil.voConvert(proTable, ProTableVo.class);
                table.setColumns(columns);
                mProFileVo.setTable(table);
            }
        }

        res.setData(mProFileVo);
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }

    @Override
    public BaseRes<String> delFileById(String fileId) {
        LOG.info("fileId={}",fileId);
        proFileMapper.deleteByPrimaryKey(fileId);

        BaseRes<String> res = new BaseRes<String>();
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }

    @Override
    public BaseRes<ProFileVo> fetchFileWithFieldByVoId(String fileId) {
        BaseRes<ProFileVo> res = new BaseRes<ProFileVo>();
        ProFile proFile = proFileMapper.selectByPrimaryKey(fileId);

        ProFileVo mProFileVo = BeanUtil.voConvert(proFile, ProFileVo.class);

        ProFieldCriteria mProFieldCriteria = new ProFieldCriteria();
        mProFieldCriteria.createCriteria().andFileIdEqualTo(fileId);
        List<ProField> proFields = proFieldMapper.selectByExample(mProFieldCriteria);

        mProFileVo.setFields(proFields);
        if (proFile.getSuperId() != null && !"".equals(proFile.getSuperId())) {

            if (com.centon.auto_code.util.mbg.Constants.FileType.VO.equals(proFile.getFileType())) {

                // fetch table
                ProTable proTable = proTableMapper.selectByPrimaryKey(proFile.getSuperId());

                ProTableColumnCriteria mProTableColumnCriteria = new ProTableColumnCriteria();
                mProTableColumnCriteria.createCriteria().andTableIdEqualTo(proTable.getId());
                List<ProTableColumn> columns = proTableColumnMapper.selectByExample(mProTableColumnCriteria);
                ProTableVo table = BeanUtil.voConvert(proTable, ProTableVo.class);
                table.setColumns(columns);
                mProFileVo.setTable(table);
            }
        }

        res.setData(mProFileVo);
        res.setServiceCode(ServiceCode.SUCCESS);
        return res;
    }

    private void updateProjectAndDatabaseRel(String projectId, List<String> dbIds) {

        // remove rel
        ProProjectDatabaseRelCriteria mProProjectDatabaseRelCriteria = new ProProjectDatabaseRelCriteria();
        mProProjectDatabaseRelCriteria.createCriteria().andProjectIdEqualTo(projectId);
        proProjectDatabaseRelMapper.deleteByExample(mProProjectDatabaseRelCriteria);

        // add rel
        for (String dbId : dbIds) {
            ProProjectDatabaseRel mProProjectDatabaseRel = new ProProjectDatabaseRel();
            mProProjectDatabaseRel.setId(CommUtils.randomUUID());
            mProProjectDatabaseRel.setCreateTime(new Date());
            mProProjectDatabaseRel.setUpdateTime(new Date());
            mProProjectDatabaseRel.setProjectId(projectId);
            mProProjectDatabaseRel.setDatabaseId(dbId);
            proProjectDatabaseRelMapper.insert(mProProjectDatabaseRel);
        }
    }
}
